home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BGETHF.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  116 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bgethf.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* local headers */
  18. #include "blkio_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      bgethf - get a header field from a block file
  23.  
  24. SYNOPSIS
  25.      #include <blkio.h>
  26.  
  27.      int bgethf(bp, offset, buf, bufsize)
  28.      BLKFILE *bp;
  29.      size_t offset;
  30.      void *buf;
  31.      size_t bufsize;
  32.  
  33. DESCRIPTION
  34.      The bgethf function reads a field from the header in the block
  35.      file associated with BLKFILE pointer bp into buf.  The field
  36.      begins offset characters from the beginning of the header and is
  37.      bufsize characters long.  buf must point to a storage area at
  38.      least bufsize characters long.
  39.  
  40.      bgethf will fail if one or more of the following is true:
  41.  
  42.      [EINVAL]       bp is not a valid BLKFILE pointer.
  43.      [EINVAL]       bn or bufsize is less than 1.
  44.      [EINVAL]       buf is the NULL pointer.
  45.      [EINVAL]       offset + bufsize extends beyond the
  46.                     boundary of the header.
  47.      [BEEOF]        bp is empty.
  48.      [BEEOF]        End of file encountered within header.
  49.      [BENOPEN]      bp is not open for reading.
  50.  
  51. SEE ALSO
  52.      bgeth, bgetbf, bputhf.
  53.  
  54. DIAGNOSTICS
  55.      Upon successful completion, a value of 0 is returned.  Otherwise,
  56.      a value of -1 is returned, and errno set to indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. #ifdef AC_PROTO
  60. int bgethf(BLKFILE *bp, size_t offset, void *buf, size_t bufsize)
  61. #else
  62. int bgethf(bp, offset, buf, bufsize)
  63. BLKFILE *bp;
  64. size_t offset;
  65. void *buf;
  66. size_t bufsize;
  67. #endif
  68. {
  69.     /* validate arguments */
  70.     if (!b_valid(bp) || buf == NULL || bufsize < 1) {
  71.         errno = EINVAL;
  72.         return -1;
  73.     }
  74.  
  75.     /* check if not open for reading */
  76.     if (!(bp->flags & BIOREAD)) {
  77.         errno = BENOPEN;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if block boundary is crossed */
  82.     if ((offset + bufsize) > bp->hdrsize) {
  83.         errno = EINVAL;
  84.         return -1;
  85.     }
  86.  
  87.     /* check if header not yet written */
  88.     if (bp->endblk < 1) {
  89.         errno = BEEOF;
  90.         return -1;
  91.     }
  92.  
  93.     /* check if not buffered */
  94.     if (bp->bufcnt == 0) {
  95.         if (b_ugetf(bp, (bpos_t)0, offset, buf, bufsize) == -1) {
  96.             BEPRINT;
  97.             return -1;
  98.         }
  99.         return 0;
  100.     }
  101.  
  102.     /* check if buffer is not loaded */
  103.     if (!(b_blockp(bp, (size_t)0)->flags & BLKREAD)) {
  104.         b_blockp(bp, (size_t)0)->bn = 0; /* read header from file */
  105.         if (b_get(bp, (size_t)0) == -1) {
  106.             BEPRINT;
  107.             return -1;
  108.         }
  109.     }
  110.  
  111.     /* copy from block buffer into buf */
  112.     memcpy(buf, ((char *)b_blkbuf(bp, (size_t)0) + offset), bufsize);
  113.  
  114.     return 0;
  115. }
  116.